package com.jincp.filepicker.ui.file;

import android.app.Activity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.app.Fragment;

import com.jincp.common.permission.MPermission;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.Constant;
import com.jincp.filepicker.bean.BaseFile;
import com.jincp.filepicker.bean.NormalFile;
import com.jincp.filepicker.base.BasePickerManager;
import com.jincp.filepicker.util.MediaFileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * =========================================================
 *
 * @ProjectName:
 * @ClassName: AudioPickerManager
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/23 15:54
 * =========================================================
 */
public class NormalFilePickerManager extends BasePickerManager<NormalFilePickerManager> {

    public static final String TAG = NormalFilePickerManager.class.getSimpleName();

    public static final int REQUEST_FILE_TAKE = 4001;
    public static final int RESULT_FILE_ITEMS = 4004;

    public static final String EXTRA_RESULT_ITEMS = "EXTRA_RESULT_ITEMS";

    private static volatile NormalFilePickerManager mInstance;

    private NormalFilePickerManager() {
    }

    public static NormalFilePickerManager getInstance() {
        if (mInstance == null) {
            synchronized (NormalFilePickerManager.class) {
                if (mInstance == null) {
                    mInstance = new NormalFilePickerManager();
                }
            }
        }
        return mInstance;
    }

    public NormalFilePickerManager reset() {
        setMultiMode(false);
        setMaxSelectedLimit(9);
        return this;
    }

    public NormalFilePickerManager setMaxSelectedLimit(int maxSelectedLimit) {
        this.maxSelectedLimit = maxSelectedLimit;
        return this;
    }

    /**
     * 选择文件的方法
     */
    public void pipickFile(Activity activity) {
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(activity, Constant.PERMISSION)) {
            Intent pickNormalFileIntent = new Intent(activity, NormalFilePickActivity.class);
            activity.startActivityForResult(pickNormalFileIntent, NormalFilePickerManager.REQUEST_FILE_TAKE);
        } else {
            MPermission.with(activity).permissions(Constant.PERMISSION).request();
        }
    }

    /**
     * 选择文件的方法
     */
    public void pipickFile(Fragment fragment) {
        if (isCleanSelectedItem()){
            clear();
        }
        if (MPermission.hasPermissions(fragment.getActivity(), Constant.PERMISSION)) {
            Intent pickNormalFileIntent = new Intent(fragment.getActivity(), NormalFilePickActivity.class);
            fragment.startActivityForResult(pickNormalFileIntent, NormalFilePickerManager.REQUEST_FILE_TAKE);
        } else {
            MPermission.with(fragment).permissions(Constant.PERMISSION).request();
        }
    }

    public void previewFile(Activity activity, NormalFile normalFile) {
        File file = new File(normalFile.getPath());
        if (!file.exists()) {
            //如果文件不存在
            ToastUtil.getInstance().show("打开失败，原因：文件已经被移动或者删除");
            return;
        }
        /* 取得扩展名 */
        String end = file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase();
        /* 依扩展名的类型决定MimeType */
        Intent intent = null;
        if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
            intent = MediaFileUtil.generateVideoAudioIntent(activity, file, MediaFileUtil.DATA_TYPE_AUDIO);
        } else if (end.equals("3gp") || end.equals("mp4")) {
            intent = MediaFileUtil.generateVideoAudioIntent(activity, file, MediaFileUtil.DATA_TYPE_VIDEO);
        } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_IMAGE);
        } else if (end.equals("apk")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_APK);
        } else if (end.equals("html") || end.equals("htm")) {
            intent = MediaFileUtil.generateHtmlFileIntent(activity, normalFile.getPath());
        } else if (end.equals("ppt")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_PPT);
        } else if (end.equals("xls")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_EXCEL);
        } else if (end.equals("doc")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_WORD);
        } else if (end.equals("pdf")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_PDF);
        } else if (end.equals("chm")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_CHM);
        } else if (end.equals("txt")) {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_TXT);
        } else {
            intent = MediaFileUtil.generateCommonIntent(activity, file, MediaFileUtil.DATA_TYPE_ALL);
        }
        activity.startActivity(intent);
    }
}